
/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.lxk.oms.flow.cancel.CancelOrderContext;
import com.lxk.oms.flow.comment.CommentOrderContext;
import com.lxk.oms.flow.confirm.ConfirmOrderContext;
import com.lxk.oms.flow.pay.PayContext;
import com.lxk.oms.flow.send.SendOrderContext;
import com.lxk.oms.order.domain.bo.OmsOrderBo;
import com.lxk.oms.order.domain.req.*;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.manger.OrderManger;
import com.lxk.oms.order.service.IOmsOrderService;
import com.lxk.service.finance.api.WalletServiceApi;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单-后台管理
 *
 * @author LXK-RY5
 * @date 2024-08-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/order")
public class OmsOrderController extends BaseController {

    private final IOmsOrderService omsOrderService;
    private final OrderManger orderManger;
    private final WalletServiceApi walletServiceApi;


    /**
     * 强制余额支付(用于平台方)
     */
    @SaCheckPermission("oms:order:payOrderForce")
    @PostMapping("/payOrderForce")
    public R<Boolean> payOrderForce(@Validated @RequestBody PayOrderReq req) {
        PayContext.PayResult result = orderManger.payForceOrder(req);
        return R.ok(result.getSuccess());
    }

    /**
     * 取消订单(用于平台方)
     */
    @SaCheckPermission("oms:order:cancelOrderForce")
    @PostMapping("/cancelOrderForce")
    public R<Boolean> cancelOrderForce(@Validated @RequestBody CancelOrderReq req) {
        CancelOrderContext.Result result = orderManger.cancelOrder(req);
        return R.ok(result.getSuccess());
    }

    /**
     * 强制评价订单(用于平台方)
     */
    @SaCheckPermission("oms:order:commentOrderForce")
    @PostMapping("/commentOrderForce")
    public R<Boolean> commentOrderForce(@Validated @RequestBody CommentOrderReq req) {
        CommentOrderContext.Result result = orderManger.commentOrder(req);
        return R.ok(result.getSuccess());
    }

    /**
     * 强制确认收货(用于平台方)
     */
    @SaCheckPermission("oms:order:confirmOrderForce")
    @PostMapping("/confirmOrderForce")
    public R<Boolean> confirmOrderForce(@Validated(AddGroup.class) @RequestBody ConfirmOrderReq sendOrderReq) {
        ConfirmOrderContext.Result result = orderManger.confirmOrder(sendOrderReq);
        return R.ok(result.getSuccess());
    }

    /**
     * 发送快递
     */
    @SaCheckPermission("oms:order:sendKd")
    @PostMapping("/sendKd")
    public R<Boolean> sendKd(@Validated @RequestBody SendOrderReq sendOrderReq) {
        SendOrderContext.SendOrderResult sendOrderResult = orderManger.sendOrder(sendOrderReq);
        return R.ok(Boolean.TRUE);
    }

    /**
     * 查询订单列表
     */
    @SaCheckPermission("oms:order:list")
    @GetMapping("/list")
    public TableDataInfo<OmsOrderVo> list(OmsOrderBo bo, PageQuery pageQuery) {
        return omsOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单列表
     */
    @SaCheckPermission("oms:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OmsOrderBo bo, HttpServletResponse response) {
        List<OmsOrderVo> list = omsOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", OmsOrderVo.class, response);
    }

    /**
     * 获取订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("oms:order:query")
    @GetMapping("/{id}")
    public R<OmsOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(omsOrderService.detail(id));
    }

    /**
     * 新增订单
     */
    @SaCheckPermission("oms:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OmsOrderBo bo) {
        return toAjax(omsOrderService.insertByBo(bo));
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("oms:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OmsOrderBo bo) {
        return toAjax(omsOrderService.updateByBo(bo));
    }

    /**
     * 删除订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("oms:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(omsOrderService.deleteWithValidByIds(List.of(ids), true));
    }
}
